window.onload = function () {

    $.ajax({
        type: 'POST',
        //url: 'http://yiyiweixin2.sinaapp.com/index.php/weixin/manage/PMcheck_list',
        url: 'http://weixin.yiyikefu.com/index.php?m=weixin&c=manage&a=PMcheck_list',
        dataType: 'json',
        error: function (xhr) {
            throw new Error('错误代码:' + xhr.status);
        },
        success: function (response) {
            createQues(response);
        }
    });

    var createQues = function (response) {
        var html = [];
        var ques = null;
        var id = null;
        for (var i = 0; i < response.length; i++) {
            id = response[i].dtitem_id;
            ques = response[i].dtitem_content;
            var string = '<div class="row">' +
                ' <span>' + ques + '</span>' +
                '<div class="input-field radio' + (i + 1) + '">' +
                ' <div class="col s4"> ' +
                '<input type="radio" name="' + id + '" id="radio0' + (i + 1) + '-01" value="1">' +
                ' <label class="btn waves-effect waves-light" for="radio0' + (i + 1) + '-01">正常</label> ' +
                '</div> ' +
                '<div class="col s4">' +
                ' <input type="radio" name="' + id + '" id="radio0' + (i + 1) + '-02" value="2"> ' +
                '<label class="btn waves-effect waves-light" for="radio0' + (i + 1) + '-02">基本正常</label> ' +
                '</div> <div class="col s4"> ' +
                '<input type="radio" name="' + id + '" id="radio0' + (i + 1) + '-03" value="3"> ' +
                '<label class="btn waves-effect waves-light" for="radio0' + (i + 1) + '-03">故障</label> ' +
                '</div> ' +
                '</div> ' +
                '</div>';
            html.push(string);
        }
        $('#fieldset').html(html);

        bindLabel(response.length);
    }

    var bindLabel = function (lenght) {
        for (var i = 0; i <= lenght; i++) {
            $('.radio' + i + ' label').click(function () {
                //移除所有选中
                $(this).parent().prevAll().children('label').removeClass('checked');
                $(this).parent().nextAll().children('label').removeClass('checked');
                //本项添加选中
                $(this).addClass('checked');
            });
        }
        $('form').submit(function (e) {
            for (var i = 1; i <= lenght; i++) {
                var checkedNum = 0;
                $('.radio' + i + ' label').each(function () {
                    if ($(this).hasClass('checked')) checkedNum++;
                });
                if (checkedNum <= 0) {
                    $('.radio' + i + ' label').addClass('bgc-red');
                    e.preventDefault();
                } else $('.radio' + i + ' label').removeClass('bgc-red');
            }
        });
    }

}
